// 用户模型
const mongo_client = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const db_name = 'dc_DB';

// 连接数据库，返回数据库对象
const db_operate = (cb) => {
    mongo_client.connect(url, (err, client) => {
        if (err) {
            console.log(err);
        }
        const db =client.db(db_name);
        cb(db);
    })
}



const insert_user = (db, obj, cb) => {
    const collection = db.collection('user');

    collection.insertOne({
        acct: obj.acct,
        pass: obj.pass,
        is_online: obj.is_online
    }, (result) => {
        cb();
    })
}

class User {
    constructor(obj) {
        for (let key in obj) {
            this[key] = obj[key];
        }
    }

    save(cb) {
        db_operate((db) => {
            insert_user(db, {
                acct: this.acct,
                pass: this.pass,
                is_online: false
            }, () => {
                console.log('a new user registed successfully.');
                cb();
            })
        })
    }

    static get_user_by_acct(acct, cb) {
        db_operate((db) => {
            const collection = db.collection('user');
            collection.find({ 'acct': acct }).toArray((err, result) => {
                if (err) {
                    console.log(err);
                    cb(err);
                } else {
                    const user = result[0];
                    cb(user);
                }
            })
        })
    }

    static authenticate(acct, pass, cb) {
        User.get_user_by_acct(acct, (result) => {
            if (typeof(result) === 'undefined') { // 用户不存在
                let state = false;
                cb(state);
            } else {
                if (pass != result.pass) {// 密码错误
                    let state = false;
                    console.log("false password")
                    cb(state);
                }
                else if(result.is_online == true){// 已登陆
                    let state = false;
                    console.log("already online")
                    cb(state);
                }
                else { 
                    let state = true;
                    cb(state);
                }
            }
        })
    }

    // 转换登录状态
    static login(acct) {
        db_operate((db) => {
            const collection = db.collection('user');
            collection.find({ 'acct': acct }).toArray((err, result) => {
                if (err) console.log(err);
                if(result[0].is_online == false)
                    collection.updateOne({ 'acct': acct }, { $set: { 'is_online': true } });
            });
        });
    }

    static logout(acct) {
        db_operate((db) => {
            const collection = db.collection('user');
            collection.find({ 'acct': acct }).toArray((err, result) => {
                if (err) console.log(err);
                if(result[0].is_online == true)
                    collection.updateOne({ 'acct': acct }, { $set: { 'is_online': false } });
                console.log("login successful");
            });
        });
    }
    // 获取用户状态
    static get_state(acct, cb) {
        db_operate((db) => {
            const collection = db.collection('user');
            collection.find({ 'acct': acct }).toArray((err, result) => {
                if (result.length == 0) { // 输入的 acct 不存在时
                    const user_info = { is_online: false, acct: acct };
                    cb(user_info);
                } else {
                    const user_info = { is_online: result[0].is_online, acct: acct };
                    cb(user_info);
                }
            });
        })
    }
}

module.exports = User;